<template>
	<view>
		<view class="heading">
			完成订单
		</view>
		<view class="box part1">
			<view class = "uni-flex uni-column">
				<view class="uni-flex uni-row">
					<view class="title">用户信息</view>
					<picker @change="changeuser" :range="user1">	
						<view class="select">{{persentuser}}</view>
					</picker>
				</view>
				<view class="cut-off-rule"></view>
				<view class="userbox">
					<view v-if="changeuserIndex != -1">
						<userlist :users='users[changeuserIndex]'></userlist>
					</view>
				</view>
			</view>
		</view>
		<view class="box part2">
			<view class="uni-flex uni-column">
				<view class="uni-flex uni-row">
					<view class="title">房源信息</view>
					<picker @change="changehouse" :range="houses">
						<view class="select">{{persenthouse}}</view>
					</picker>
				</view>
			</view>
			<view class="cut-off-rule"></view>
			<view class="housebox">
				<view v-if="changehouseIndex != -1" >
					<rentinglist v-if="houseInfo[changehouseIndex].rentingHeading" :rent='houseInfo[changehouseIndex]'></rentinglist>
					<sellhouselist v-if="houseInfo[changehouseIndex].sellHeading" :sell='houseInfo[changehouseIndex]'></sellhouselist>
				</view>
			</view>
		</view>
		<button class="add" @tap = "confirm">添加订单</button>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	import userlist from '../common/userlist.vue';
	import rentinglist from '../common/rentinglist.vue'
	import sellhouselist from '../common/sellhouselist.vue'
	export default {
		components:{
			userlist,
			rentinglist,
			sellhouselist
		},
		data() {
			return {
				user1:[],
				houses:[],
				houseInfo:[],
				changeuserIndex:-1,
				changehouseIndex:-1,
				persentuser:'选择用户',
				persenthouse:'选择房源'
			}
		},
		methods: {
			changeuser(e) {
				this.changeuserIndex = e.target.value;
				this.persentuser=this.user1[this.changeuserIndex]
			},
			changehouse(h){
				this.changehouseIndex = h.target.value;
				this.persenthouse = this.houses[this.changehouseIndex]
			},
			getAllUsers(){
				this.$axios.post('getAllUsers')
				.then(res => {
					this.users = res.data.data
					for(let i = 0;i<this.users.length;i++)
					{
						this.user1[i] = '用户编号:'+ this.users[i].userId
					}
					this.$forceUpdate()
				})
			},
			gethouses(){
				// this.$axios.post('getAllRentings')
				// .then(res => {
				// 	this.renting = res.data.allRentings
				// 	this.retingnum = this.renting.length
				// 	for(let i = 0 ; i < this.renting.length ; i++)
				// 	{
				// 		this.house.push('房源类型:租房  房源编号:'+this.renting[i].houseId)
				// 	}
				// 	console.log(this.house)
				// 	this.$forceUpdate()
				// })
				this.$axios.post('getAllHouses')
				.then(res =>{
					this.house = res.data.allHouses
					this.getRenting()
					this.getSelling()
				})
			},
			getRenting(){
				this.$axios.post('getAllRentings')
                .then(res =>{
                    this.rentingHouse = res.data.allRentings
					this.sellnum = this.rentingHouse.length
                    for(let j=0 ; j < this.house.length ; j++){
                        for(let k=0 ; k < this.rentingHouse.length ; k++){
                            if(this.rentingHouse[k].houseId == this.house[j].houseId){
                                let temporary = {}
                                temporary.houseId = this.rentingHouse[k].houseId
								temporary.userId = this.house[j].userId
                                temporary.rentingHeading = this.rentingHouse[k].rentingHeading
                                temporary.rentingPrice = this.rentingHouse[k].rentingPrice
                                temporary.houseEstate = this.house[j].houseEstate
                                temporary.houseArea = this.house[j].houseArea
                                temporary.houseSurfaceplot = this.house[j].houseSurfaceplot
                                temporary.houseDirection = this.house[j].houseDirection
                                temporary.houseRegion = this.house[j].houseRegion
                                temporary.houseCity = this.house[j].houseCity
								this.houseInfo.push(temporary)
								this.houses.push('房源编号:'+this.house[j].houseId)
                            }
                        }
					}
					
					this.$forceUpdate()
				})
			},
			getSelling(){
				this.$axios.post('getAllSells')
                .then(res =>{
                    this.selling = res.data.allSells
                    for(let j=0 ; j < this.house.length ; j++){
                        for(let k=0 ; k < this.selling.length ; k++){
                            if( this.house[j].houseId == this.selling[k].houseId){
                                let temporary = {}
                                temporary.houseId = this.house[j].houseId
								temporary.userId = this.house[j].userId
                                temporary.sellHeading = this.selling[k].sellHeading
                                temporary.sellPrice = this.selling[k].sellPrice
                                temporary.houseArea = this.house[j].houseArea
                                temporary.houseSurfaceplot = this.house[j].houseSurfaceplot
                                temporary.houseDirection = this.house[j].houseDirection
                                temporary.houseEstate = this.house[j].houseEstate
								this.houseInfo.push(temporary)
								this.houses.push('房源编号:'+this.house[j].houseId)
                            }
                        }
                    }
                    this.$forceUpdate()
                })
			},
			confirm(){
				uni.showModal({  
					title:"确认添加", 
					content:this.user1[this.changeuserIndex]+'\n'+this.houses[this.changehouseIndex],
					showCancel:true,
					success: (res) => {
						// console.log(this.user.userId)
						this.addForm()
						// console.log(this.houseInfo[this.changehouseIndex])
						// console.log(this.users[this.changeuserIndex])
						// console.log(this.houseInfo[this.changehouseIndex].rentingHeading ? 2 : 1) 
					} 
				});
			},
			addForm(){
				this.$axios.post('addForm', this.qs.stringify({
					houseId: this.houseInfo[this.changehouseIndex].houseId,
					formType: this.houseInfo[this.changehouseIndex].rentingHeading ? 2 : 1,
					formBuyerid: this.users[this.changeuserIndex].userId,
					formSellerid: this.houseInfo[this.changehouseIndex].userId,
					agentId: this.user.userId,
				}))
				.then(res =>{
                    uni.showToast({
                    title: '申请成功',
                    duration: 2000
                    });
                })
                .catch(err => {
                    console.log(err)
                })
			}
		},
		mounted() {
			this.getAllUsers()
			this.gethouses()
		},
		computed:{
			...mapState(['user'])
		}
	}
</script>

<style>
.heading{
	text-align: center;
	font-size: 140%;
	padding: 20rpx 10rpx;
}

.box{
	width: 100%;
	border: 2px solid rgb(231, 231, 231);
  	border-radius: 8px;
	margin: 30rpx 0;
}

.part1{
	height: 400rpx;
}
 
.title{
	font-size: 130%;
	padding: 20rpx 10rpx;
}

.select{
	text-align: center;
	margin: 22rpx;
	background-color: rgb(231, 231, 231);
	border-radius: 8px;
}
.userbox{
	padding: 20rpx;
	height: 200rpx;
	width: 100%;
}

.housebox{
	padding: 20rpx 10rpx;
	height: 350rpx;
	width: 100%;
}

.add{
	color: white;
	background-color: rgb(235, 88, 88);
	font-size: 130%;
	width: 90%;
	margin: 100rpx 40rpx;
}

.cut-off-rule{
	height: 1rpx;
	width: 80%;
	background-color: rgb(224, 223, 223);
	margin: 20rpx 10%;
}

</style>
